﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Core;
using XcoAppSpaces.Dialogs;

namespace test.seralization
{
    class Program
    {
        static void Main(string[] args)
        {
            using(XcoAppSpace server = new XcoAppSpace("tcp.port=12345"))
            using(XcoAppSpace client = new XcoAppSpace("tcp.port=0"))
            {
                server.RunWorker<Port<Subscribe<int>>, MyPublisher>("publisher");
                Port<Subscribe<int>> pub = client.ConnectWorker<Port<Subscribe<int>>>("localhost:12345/publisher");

                //DialogHost dh = new DialogHost(client.DefaultDispatcherQueue);
                //Port<TheMsg> pRemote = new Port<TheMsg>();
                //dh.StartDialog(new TheMsg {Value = 99}, new MyDialog(pub), pRemote);

                Port<int> sub = new Port<int>();
                int j = 1;
                pub.Post(new Subscribe<int>(sub, i => i==j));

                Console.ReadLine();
            }
        }
    }

    class MyPublisher : Port<Subscribe<int>>
    {
        [XcoConcurrent]
        void Process(Subscribe<int> subscription)
        {
            Console.WriteLine("received sub");
        }
    }


    class MyDialog : Dialog
    {
        private Port<Subscribe<int>> publisher;
        private Port<int> subscription;

        public MyDialog(Port<Subscribe<int>> publisher)
        {
            this.publisher = publisher;
            this.subscription = new Port<int>();
        }

        protected override void Define()
        {
            Handler<TheMsg>(m => this.publisher.Post(new Subscribe<int>(this.subscription, 
                                                                        i => i == 99)));
        }
    }

    class TheMsg
    {
        public int Value = 0;
    }
}
